/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is
 *
 * java-concurrency-presentation:
 * concurrency presentation tutorial.
 *
 * The Initial Developer of the Original Code is
 * Marco Antonio Villalobos, Jr.
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): None.
 *
 * ***** END LICENSE BLOCK *****
 */

package presentation.concurrency.slides;

import java.io.*;

/**
 * This example shows how to use a PipedInputStream and
 * PipedOutputStream.
 *
 * If an application must process data with streams,
 * then creating an output stream and reading it with an input
 * stream would be impossible in a single threaded application
 * because of the serial nature of writing and reading.
 *
 * With this approach, one thread does writing, while another does the
 * reading.
 *
 * Most application developers find a way to do this with shared
 * memory instead.
 */
public class Pipe {

    /**
     * Uses a thread, and PipedOutputStream to return a PipedInputStream.
     *
     * @return An input stream in which data is coming from another thread using pipes.
     * @throws IOException When there is an IO Error.
     */
    public InputStream getInputStream() throws IOException {
        final PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        Thread thread = new Thread() {
            public void run() {
                try {
                    for (int i=0;i<1024;i++) {
                        pos.write(i);
                    }
                    pos.write(-1);
                } catch (IOException e) {
                    /// ... handle exception
                } finally {
                    try {
                        pos.close();
                    } catch (IOException e) {
                        //ignore
                    }
                }
            }
        };
        thread.start();
        return pis;
    }

    /**
     * Makes actual usage of this example.
     */
    public static void main(String[] args) {
        Pipe pipe = new Pipe();
        InputStream is = null;
        try {
            is = pipe.getInputStream();
            int current;
            int i=0;
            while ((current = is.read()) != -1) {
                System.out.println((i++)+":"+current);
            }
        } catch (IOException e) {
            // ... handle exception
        } finally {
            if (is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    //ignore
                }
            }
        }
    }
}
